﻿using MediatR;
using Traceability.App.Api.Application.Command.Detail.Cultivation;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Cultivation;

namespace Traceability.App.Api.Application.Handler.Detail.Cultivation
{
    public class DelImmunityHandler : IRequestHandler<DelImmunityCommand, ApiResult<int>>
    {
        IBaseRepository<ImmunityModel> immRepo;


        public DelImmunityHandler(IBaseRepository<ImmunityModel> immRepo)
        {
            this.immRepo = immRepo;

        }
        public Task<ApiResult<int>> Handle(DelImmunityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var imm = immRepo.GetAll().Where(c => c.Id == request.Id).FirstOrDefault();
            if (imm == null)
            {
                res.Msg = "您要删除的内容不存在";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            imm.IsDeleted = true;
            int a = immRepo.Update(imm).Result;
            if (a > 0)
            {
                res.Msg = "免疫信息删除成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "免疫信息删除失败";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
